package com.example.flatlafdemo.components.mainPanel;

import com.example.flatlafdemo.action.TaskAction;
import com.example.flatlafdemo.utils.IconUtil;
import com.example.flatlafdemo.utils.StringUtil;
import org.jdesktop.swingx.JXTaskPane;
import org.jdesktop.swingx.JXTaskPaneContainer;

import javax.swing.*;
import java.awt.*;

public class MainPanel extends JPanel {

    private JPanel contentPanel;

    public MainPanel() {
        setLayout(new BorderLayout(5, 5));
        initMainPanel();
    }

    public void updateContentPanel(Container container) {
        if (contentPanel == null) {
            return;
        }
        Component[] components = contentPanel.getComponents();
        for (Component component : components) {
            contentPanel.remove(component);
        }

        contentPanel.add(container, BorderLayout.CENTER);
        contentPanel.revalidate();
    }

    private void initMainPanel() {
        JPanel leftPanel = initLeftPanel();
        JPanel rightPanel = initRightPanel();

        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
        add(splitPane, BorderLayout.CENTER);
    }

    private JPanel initLeftPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createTitledBorder("控件列表"));

        JXTaskPaneContainer tpc = new JXTaskPaneContainer();

        JXTaskPane toplevelPane = new JXTaskPane("Toplevel Containers");
        toplevelPane.add(new TaskAction(StringUtil.Frame_Demo, IconUtil.Frame_Demo_Icon));
        toplevelPane.add(new TaskAction(StringUtil.Dialog_Demo, IconUtil.Dialog_Demo_Icon));
        toplevelPane.add(new TaskAction(StringUtil.Window_Demo, IconUtil.Window_Demo_Icon));
        tpc.add(toplevelPane);

        JXTaskPane containersPane = new JXTaskPane("Containers");
        containersPane.add(new TaskAction(StringUtil.InternalFrame_Demo, IconUtil.InternalFrame_Demo_Icon));
        containersPane.add(new TaskAction(StringUtil.TabbedPane_Demo, IconUtil.TabbedPane_Demo_Icon));
        containersPane.add(new TaskAction(StringUtil.ScrollPane_Demo, IconUtil.ScrollPane_Demo_Icon));
        containersPane.add(new TaskAction(StringUtil.SplitPane_Demo, IconUtil.SplitPane_Demo_Icon));
        tpc.add(containersPane);

        JXTaskPane dataPane = new JXTaskPane("Data");
        dataPane.add(new TaskAction(StringUtil.Table_Demo, IconUtil.Table_Demo_Icon));
        dataPane.add(new TaskAction(StringUtil.Tree_Demo, IconUtil.Tree_Demo_Icon));
        dataPane.add(new TaskAction(StringUtil.List_Demo, IconUtil.List_Demo_Icon));
        tpc.add(dataPane);

        JXTaskPane controlsPane = new JXTaskPane("Controls");
        controlsPane.add(new TaskAction(StringUtil.ToggleButton_Demo, IconUtil.ToggleButton_Demo_Icon));
        controlsPane.add(new TaskAction(StringUtil.Button_Demo, IconUtil.Button_Demo_Icon));
        controlsPane.add(new TaskAction(StringUtil.ComboBox_Demo, IconUtil.ComboBox_Demo_Icon));
        controlsPane.add(new TaskAction(StringUtil.ProgressBar_Demo, IconUtil.ProgressBar_Demo_Icon));
        controlsPane.add(new TaskAction(StringUtil.Slider_Demo, IconUtil.Slider_Demo_Icon));
        controlsPane.add(new TaskAction(StringUtil.Spinner_Demo, IconUtil.Spinner_Demo_Icon));
        tpc.add(controlsPane);

        JXTaskPane textPane = new JXTaskPane("Text");
        textPane.add(new TaskAction(StringUtil.TextField_Demo, IconUtil.TextField_Demo_Icon));
        textPane.add(new TaskAction(StringUtil.EditorPane_Demo, IconUtil.EditorPane_Demo_Icon));
        tpc.add(textPane);

        JXTaskPane choosersPane = new JXTaskPane("Choosers");
        choosersPane.add(new TaskAction(StringUtil.ColorChooser_Demo, IconUtil.ColorChooser_Demo_Icon));
        choosersPane.add(new TaskAction(StringUtil.OptionPane_Demo, IconUtil.OptionPane_Demo_Icon));
        tpc.add(choosersPane);

        JXTaskPane generalPane = new JXTaskPane("General");
        generalPane.add(new TaskAction(StringUtil.ToolTip_Demo, IconUtil.ToolTip_Demo_Icon));
        tpc.add(generalPane);

        panel.add(new JScrollPane(tpc), BorderLayout.CENTER);
        return panel;
    }

    private JPanel initRightPanel() {
        contentPanel = new JPanel(new BorderLayout());
        contentPanel.setBorder(BorderFactory.createTitledBorder("控件显示"));
        return contentPanel;
    }
}
